/*     */ package com.zimbra.cs.mailbox;
/*     */ 
/*     */ import com.google.common.base.Objects;
/*     */ import com.google.common.base.Objects.ToStringHelper;
/*     */ import com.google.common.base.Strings;
/*     */ import com.zimbra.common.localconfig.KnownKey;
/*     */ import com.zimbra.common.localconfig.LC;
/*     */ import com.zimbra.common.mailbox.Color;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.db.DbMailItem;
/*     */ import com.zimbra.cs.index.IndexDocument;
/*     */ import com.zimbra.cs.mime.ParsedDocument;
/*     */ import com.zimbra.cs.store.MailboxBlob;
/*     */ import com.zimbra.cs.store.StagedBlob;
/*     */ import java.io.IOException;
/*     */ import java.util.ArrayList;
/*     */ import java.util.List;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class Document
/*     */   extends MailItem
/*     */ {
/*     */   protected String contentType;
/*     */   protected String creator;
/*     */   protected String fragment;
/*     */   protected String lockOwner;
/*     */   protected long lockTimestamp;
/*     */   protected String description;
/*     */   protected boolean descEnabled;
/*     */   private static final String CN_FRAGMENT = "fragment";
/*     */   private static final String CN_MIME_TYPE = "mime_type";
/*     */   private static final String CN_FILE_NAME = "filename";
/*     */   private static final String CN_EDITOR = "edited_by";
/*     */   private static final String CN_LOCKOWNER = "locked_by";
/*     */   private static final String CN_LOCKTIMESTAMP = "locked_at";
/*     */   private static final String CN_DESCRIPTION = "description";
/*     */   
/*     */   public Document(Mailbox mbox, MailItem.UnderlyingData data)
/*     */     throws ServiceException
/*     */   {
/*  51 */     this(mbox, data, false);
/*     */   }
/*     */   
/*     */   public Document(Mailbox mbox, MailItem.UnderlyingData data, boolean skipCache) throws ServiceException {
/*  55 */     super(mbox, data, skipCache);
/*     */   }
/*     */   
/*     */   public String getContentType() {
/*  59 */     return this.contentType;
/*     */   }
/*     */   
/*     */   public String getSender()
/*     */   {
/*  64 */     return getCreator();
/*     */   }
/*     */   
/*     */   public String getCreator() {
/*  68 */     return Strings.nullToEmpty(this.creator);
/*     */   }
/*     */   
/*     */   public String getFragment() {
/*  72 */     return Strings.nullToEmpty(this.fragment);
/*     */   }
/*     */   
/*     */   public String getLockOwner() {
/*  76 */     return this.lockOwner;
/*     */   }
/*     */   
/*     */   public long getLockTimestamp() {
/*  80 */     return this.lockTimestamp;
/*     */   }
/*     */   
/*     */   public String getDescription() {
/*  84 */     return Strings.nullToEmpty(this.description);
/*     */   }
/*     */   
/*     */   public boolean isDescriptionEnabled() {
/*  88 */     return this.descEnabled;
/*     */   }
/*     */   
/*     */   boolean isTaggable()
/*     */   {
/*  93 */     return true;
/*     */   }
/*     */   
/*     */   boolean isCopyable()
/*     */   {
/*  98 */     return true;
/*     */   }
/*     */   
/*     */   boolean isMovable()
/*     */   {
/* 103 */     return true;
/*     */   }
/*     */   
/*     */   boolean isMutable()
/*     */   {
/* 108 */     return true;
/*     */   }
/*     */   
/*     */   boolean canHaveChildren()
/*     */   {
/* 113 */     return true;
/*     */   }
/*     */   
/*     */   int getMaxRevisions() throws ServiceException
/*     */   {
/* 118 */     return getAccount().getIntAttr("zimbraNotebookMaxRevisions", 0);
/*     */   }
/*     */   
/*     */   public List<IndexDocument> generateIndexData() throws MailItem.TemporaryIndexingException
/*     */   {
/*     */     try {
/* 124 */       MailboxBlob mblob = getBlob();
/* 125 */       if (mblob == null) {
/* 126 */         ZimbraLog.index.warn("Unable to fetch blob for Document id=%d,ver=%d,vol=%s", new Object[] { Integer.valueOf(this.mId), Integer.valueOf(this.mVersion), getLocator() });
/*     */         
/* 128 */         throw new MailItem.TemporaryIndexingException();
/*     */       }
/*     */       
/* 131 */       ParsedDocument pd = null;
/* 132 */       pd = new ParsedDocument(mblob.getLocalBlob(), getName(), getContentType(), getChangeDate(), getCreator(), getDescription(), isDescriptionEnabled());
/*     */       
/*     */ 
/* 135 */       if (pd.hasTemporaryAnalysisFailure()) {
/* 136 */         throw new MailItem.TemporaryIndexingException();
/*     */       }
/*     */       
/* 139 */       IndexDocument doc = pd.getDocument();
/* 140 */       if (doc != null) {
/* 141 */         List<IndexDocument> toRet = new ArrayList(1);
/* 142 */         toRet.add(doc);
/* 143 */         return toRet;
/*     */       }
/* 145 */       return new ArrayList(0);
/*     */     }
/*     */     catch (IOException e) {
/* 148 */       ZimbraLog.index.warn("Error generating index data for Wiki Document " + getId() + ". Item will not be indexed", e);
/* 149 */       return new ArrayList(0);
/*     */     } catch (ServiceException e) {
/* 151 */       ZimbraLog.index.warn("Error generating index data for Wiki Document " + getId() + ". Item will not be indexed", e); }
/* 152 */     return new ArrayList(0);
/*     */   }
/*     */   
/*     */   public void reanalyze(Object obj, long newSize)
/*     */     throws ServiceException
/*     */   {
/* 158 */     if (!(obj instanceof ParsedDocument)) {
/* 159 */       throw ServiceException.FAILURE("cannot reanalyze non-ParsedDocument object", null);
/*     */     }
/* 161 */     if (this.mData.isSet(Flag.FlagInfo.UNCACHED)) {
/* 162 */       throw ServiceException.FAILURE("cannot reanalyze an old item revision", null);
/*     */     }
/* 164 */     ParsedDocument pd = (ParsedDocument)obj;
/*     */     
/*     */ 
/* 167 */     markItemModified(524288);
/*     */     
/*     */ 
/* 170 */     if (!this.mData.name.equals(pd.getFilename())) {
/* 171 */       markItemModified(4096);
/*     */     }
/*     */     
/* 174 */     this.contentType = pd.getContentType();
/* 175 */     this.creator = pd.getCreator();
/*     */     
/* 177 */     if (!LC.documents_disable_instant_parsing.booleanValue()) {
/* 178 */       this.fragment = pd.getFragment();
/*     */     }
/* 180 */     this.mData.date = ((int)(pd.getCreatedDate() / 1000L));
/* 181 */     this.mData.name = pd.getFilename();
/* 182 */     this.mData.setSubject(pd.getFilename());
/* 183 */     this.description = pd.getDescription();
/* 184 */     this.descEnabled = pd.isDescriptionEnabled();
/* 185 */     pd.setVersion(getVersion());
/*     */     
/* 187 */     if (this.mData.size != pd.getSize()) {
/* 188 */       markItemModified(16);
/* 189 */       this.mMailbox.updateSize(pd.getSize() - this.mData.size, false);
/* 190 */       getFolder().updateSize(0, 0, pd.getSize() - this.mData.size);
/* 191 */       this.mData.size = pd.getSize();
/*     */     }
/*     */     
/* 194 */     saveData(new DbMailItem(this.mMailbox));
/*     */   }
/*     */   
/*     */   protected static MailItem.UnderlyingData prepareCreate(MailItem.Type type, int id, String uuid, Folder folder, String name, String mimeType, ParsedDocument pd, Metadata meta, MailItem.CustomMetadata custom, int flags)
/*     */     throws ServiceException
/*     */   {
/* 200 */     return prepareCreate(type, id, uuid, folder, name, mimeType, pd, meta, custom, flags, LC.documents_disable_instant_parsing.booleanValue());
/*     */   }
/*     */   
/*     */   protected static MailItem.UnderlyingData prepareCreate(MailItem.Type type, int id, String uuid, Folder folder, String name, String mimeType, ParsedDocument pd, Metadata meta, MailItem.CustomMetadata custom, int flags, boolean skipParsing) throws ServiceException
/*     */   {
/* 205 */     if ((folder == null) || (!folder.canContain(MailItem.Type.DOCUMENT))) {
/* 206 */       throw MailServiceException.CANNOT_CONTAIN();
/*     */     }
/* 208 */     if (!folder.canAccess((short)4)) {
/* 209 */       throw ServiceException.PERM_DENIED("you do not have the required rights on the folder");
/*     */     }
/* 211 */     name = validateItemName(name);
/*     */     
/* 213 */     MailItem.CustomMetadata.CustomMetadataList extended = custom == null ? null : custom.asList();
/*     */     
/* 215 */     Mailbox mbox = folder.getMailbox();
/*     */     
/* 217 */     MailItem.UnderlyingData data = new MailItem.UnderlyingData();
/* 218 */     data.uuid = uuid;
/* 219 */     data.id = id;
/* 220 */     data.type = type.toByte();
/* 221 */     data.folderId = folder.getId();
/* 222 */     if ((!folder.inSpam()) || (mbox.getAccount().getBooleanAttr("zimbraJunkMessagesIndexingEnabled", false))) {
/* 223 */       data.indexId = MailItem.IndexStatus.DEFERRED.id();
/*     */     }
/* 225 */     data.imapId = id;
/* 226 */     data.date = ((int)(pd.getCreatedDate() / 1000L));
/* 227 */     data.size = pd.getSize();
/* 228 */     data.name = name;
/* 229 */     data.setSubject(name);
/* 230 */     data.setBlobDigest(pd.getDigest());
/* 231 */     data.metadata = encodeMetadata(meta, DEFAULT_COLOR_RGB, 1, 1, extended, mimeType, pd.getCreator(), skipParsing ? null : pd.getFragment(), null, 0L, pd.getDescription(), pd.isDescriptionEnabled(), null).toString();
/*     */     
/* 233 */     data.setFlags(flags);
/* 234 */     return data;
/*     */   }
/*     */   
/*     */   static Document create(int id, String uuid, Folder folder, String filename, String type, ParsedDocument pd, MailItem.CustomMetadata custom, int flags) throws ServiceException
/*     */   {
/* 239 */     assert (id != -1);
/*     */     
/* 241 */     Mailbox mbox = folder.getMailbox();
/* 242 */     MailItem.UnderlyingData data = prepareCreate(MailItem.Type.DOCUMENT, id, uuid, folder, filename, type, pd, null, custom, flags);
/* 243 */     data.contentChanged(mbox);
/*     */     
/* 245 */     ZimbraLog.mailop.info("Adding Document %s: id=%d, folderId=%d, folderName=%s", new Object[] { filename, Integer.valueOf(data.id), Integer.valueOf(folder.getId()), folder.getName() });
/*     */     
/* 247 */     new DbMailItem(mbox).create(data);
/*     */     
/* 249 */     Document doc = new Document(mbox, data);
/* 250 */     doc.finishCreation(null);
/* 251 */     pd.setVersion(doc.getVersion());
/* 252 */     return doc;
/*     */   }
/*     */   
/*     */   void decodeMetadata(Metadata meta)
/*     */     throws ServiceException
/*     */   {
/* 258 */     MetadataList revlist = meta.getList("rev", true);
/* 259 */     if ((revlist != null) && (!revlist.isEmpty())) {
/*     */       try {
/* 261 */         Metadata rev = revlist.getMap(revlist.size() - 1);
/* 262 */         this.creator = rev.get("cr", null);
/* 263 */         this.fragment = rev.get("f", null);
/*     */         
/* 265 */         int version = (int)rev.getLong("ver", 1L);
/* 266 */         if ((version > 1) && (rev.getLong("ver", 1L) != 1L)) {
/* 267 */           meta.put("ver", version);
/*     */         }
/*     */       }
/*     */       catch (ServiceException ignored) {}
/*     */     }
/*     */     
/* 273 */     super.decodeMetadata(meta);
/*     */     
/* 275 */     this.contentType = meta.get("ct");
/* 276 */     this.creator = meta.get("cr", this.creator);
/* 277 */     this.fragment = meta.get("f", this.fragment);
/* 278 */     this.lockOwner = meta.get("lo", this.lockOwner);
/* 279 */     this.description = meta.get("de", this.description);
/* 280 */     this.lockTimestamp = meta.getLong("lt", 0L);
/* 281 */     this.descEnabled = meta.getBool("dee", true);
/*     */   }
/*     */   
/*     */   Metadata encodeMetadata(Metadata meta)
/*     */   {
/* 286 */     return encodeMetadata(meta, this.mRGBColor, this.mMetaVersion, this.mVersion, this.mExtendedData, this.contentType, this.creator, this.fragment, this.lockOwner, this.lockTimestamp, this.description, this.descEnabled, this.rights);
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   static Metadata encodeMetadata(Metadata meta, Color color, int metaVersion, int version, MailItem.CustomMetadata.CustomMetadataList extended, String mimeType, String creator, String fragment, String lockowner, long lockts, String description, boolean descEnabled, ACL rights)
/*     */   {
/* 293 */     if (meta == null) {
/* 294 */       meta = new Metadata();
/*     */     }
/* 296 */     meta.put("ct", mimeType);
/* 297 */     meta.put("cr", creator);
/* 298 */     meta.put("f", fragment);
/* 299 */     meta.put("lo", lockowner);
/* 300 */     meta.put("lt", lockts);
/* 301 */     meta.put("de", description);
/* 302 */     meta.put("dee", descEnabled);
/* 303 */     return MailItem.encodeMetadata(meta, color, rights, metaVersion, version, extended);
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public String toString()
/*     */   {
/* 316 */     Objects.ToStringHelper helper = Objects.toStringHelper(this);
/* 317 */     helper.add("type", getType());
/* 318 */     helper.add("filename", getName());
/* 319 */     helper.add("edited_by", getCreator());
/* 320 */     helper.add("mime_type", this.contentType);
/* 321 */     appendCommonMembers(helper);
/* 322 */     helper.add("fragment", this.fragment);
/* 323 */     if (this.description != null) {
/* 324 */       helper.add("description", this.description);
/*     */     }
/* 326 */     if (this.lockOwner != null) {
/* 327 */       helper.add("locked_by", this.lockOwner);
/*     */     }
/* 329 */     if (this.lockTimestamp > 0L) {
/* 330 */       helper.add("locked_at", this.lockTimestamp);
/*     */     }
/* 332 */     return helper.toString();
/*     */   }
/*     */   
/*     */   protected boolean trackUserAgentInMetadata()
/*     */   {
/* 337 */     return true;
/*     */   }
/*     */   
/*     */   MailboxBlob setContent(StagedBlob staged, Object content) throws ServiceException, IOException
/*     */   {
/* 342 */     checkLock();
/* 343 */     return super.setContent(staged, content);
/*     */   }
/*     */   
/*     */   boolean move(Folder target) throws ServiceException
/*     */   {
/* 348 */     checkLock();
/* 349 */     return super.move(target);
/*     */   }
/*     */   
/*     */   void rename(String name, Folder target) throws ServiceException
/*     */   {
/* 354 */     String oldName = getName();
/* 355 */     super.rename(name, target);
/* 356 */     if (!oldName.equalsIgnoreCase(name)) {
/* 357 */       this.mMailbox.index.add(this);
/*     */     }
/*     */   }
/*     */   
/*     */   void lock(Account authuser) throws ServiceException {
/* 362 */     if ((this.lockOwner != null) && (!this.lockOwner.equalsIgnoreCase(authuser.getId()))) {
/* 363 */       throw MailServiceException.CANNOT_LOCK(this.mId, this.lockOwner);
/*     */     }
/* 365 */     this.lockOwner = authuser.getId();
/* 366 */     this.lockTimestamp = System.currentTimeMillis();
/* 367 */     markItemModified(8388608);
/* 368 */     saveMetadata();
/*     */   }
/*     */   
/*     */   void unlock(Account authuser) throws ServiceException
/*     */   {
/* 373 */     if (this.lockOwner == null) {
/* 374 */       return;
/*     */     }
/* 376 */     if ((!this.lockOwner.equalsIgnoreCase(authuser.getId())) && (checkRights((short)256, authuser, false) == 0)) {
/* 377 */       throw MailServiceException.CANNOT_UNLOCK(this.mId, this.lockOwner);
/*     */     }
/* 379 */     this.lockOwner = null;
/* 380 */     this.lockTimestamp = 0L;
/* 381 */     markItemModified(8388608);
/* 382 */     saveMetadata();
/*     */   }
/*     */   
/*     */   protected void checkLock() throws ServiceException {
/* 386 */     if ((this.lockOwner != null) && (!this.mMailbox.getLockAccount().getId().equalsIgnoreCase(this.lockOwner))) {
/* 387 */       throw MailServiceException.LOCKED(this.mId, this.lockOwner);
/*     */     }
/*     */   }
/*     */   
/*     */   MailItem.PendingDelete getDeletionInfo() throws ServiceException
/*     */   {
/* 393 */     MailItem.PendingDelete info = super.getDeletionInfo();
/* 394 */     for (Comment comment : this.mMailbox.getComments(null, this.mId, 0, -1, inDumpster())) {
/* 395 */       info.add(comment.getDeletionInfo());
/*     */     }
/* 397 */     return info;
/*     */   }
/*     */   
/*     */   protected long getMaxAllowedExternalShareLifetime(Account account)
/*     */   {
/* 402 */     return account.getFileExternalShareLifetime();
/*     */   }
/*     */   
/*     */   protected long getMaxAllowedInternalShareLifetime(Account account)
/*     */   {
/* 407 */     return account.getFileShareLifetime();
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/mailbox/Document.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */